// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_detail = require("./accountRecordApply.css");
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化

	var init = function() {
		router.init();
		showList("inquireNormalPickupApplicationPageList.json");
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}

		$("#applyType span").click(function() {
			$("#applyType span").removeClass("active_applyType");
			$(this).addClass("active_applyType");
			$(this).parent().removeClass("applyType1").removeClass("applyType2").removeClass("applyType3").removeClass("applyType4").removeClass("applyType5").removeClass("applyType6");
			$(this).parent().addClass($(this).attr("applytype"));
			$("#accountRecordApplySearchButton").attr("action", $(this).attr("action"));
			showList($(this).attr("action"));
		});

		$("#accountRecordApplySearchButton").unbind("click").click(function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化				
			showList($(this).attr("action"));
		});
	};

	//申请列表
	var showList = function(action) {
		if ($("#financeStateApply").val() != -1) {
			var financeState = $("#financeStateApply").val();
		}
		var dateBegin = $("#dateBegin").val();
		var dateEnd = $("#dateEnd").val();
		base.fnAjax({
			action: action,
			async: false,
			oData: {
				pageSize: pageSize,
				pageIndex: pageIndex,
				applicationState: financeState,
				financeNumber: $("#applyFinanceNumber").val(),
				dateBegin:dateBegin,
				dateEnd:dateEnd
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var applyType = $(".active_applyType").attr("applyType");
					juicer.register("getValueByKey", constant.getValueByKey);
					if(applyType == "applyType3" || applyType == "applyType5" || applyType == "applyType6"){
						var tpl = $("#accountRecordApplyListTpl1").html();
					}else{
						var tpl = $("#accountRecordApplyListTpl").html();
					}
					var html = juicer(tpl, data);
					$("#accountRecordApplyListBody").html(html);
					$(".cancelApplication").unbind("click").click(function() {
					});

					$(".detailApplication").unbind("click").click(function() {
						if (applyType == "applyType1") {
							router.route("account/accountPickupDetail.html?action=inquireNormalPickupApplicationDetail.json&pickupId=" + $(this).parents("tr").attr("pickupId"));
						} else if (applyType == "applyType2") {
							router.route("account/accountPickupDetail.html?action=inquirePrePickupApplicationDetail.json&pickupId=" + $(this).parents("tr").attr("pickupId"));
						} else if (applyType == "applyType3") {
							router.route("account/accountPickupDelayApplicationDetail.html?pickupDelayId=" + $(this).parents("tr").attr("pickupDelayId"));
						} else if (applyType == "applyType4") {
							router.route("account/accountPickupDetail.html?action=inquireRansomPickupApplicationDetail.json&pickupId=" + $(this).parents("tr").attr("pickupId"));
						} else if (applyType == "applyType5") {
							router.route("account/accountDetainedApplicationDetail.html?detainedId="+$(this).parents("tr").attr("detainedId"));
						} else if (applyType == "applyType6") {
							router.route("account/interestDelayApplicationDetail.html?interestDelayId="+$(this).parents("tr").attr("interestDelayId"));
						}
					});
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#changepage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							showList(action);
						});
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});